//
//  LabelStyle.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/5/31.
//  Copyright © 2019 Fanxx. All rights reserved.
//

import UIKit

extension UCSetting where TargetType: UILabel {
    ///文本
    public static func text(_ text:String?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.text = text
        })
    }
    public static func text(_ text:NSAttributedString?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.attributedText = text
        })
    }
    ///字体
    public static func font(_ font:UIFont?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.font = font
        })
    }
    ///系统字体
    public static var systemFont:UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: UIFont.systemFontSize))}
    ///系统字体
    public static func font(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size))}
    ///系统字体
    public static func font(size: CGFloat, weight: UIFont.Weight) -> UCSetting<TargetType> { return .font(UIFont.systemFont(ofSize: size, weight: weight))}
    ///系统字体
    public static func boldFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.boldSystemFont(ofSize: size))}
    ///系统字体
    public static func italicFont(size: CGFloat) -> UCSetting<TargetType> { return .font(UIFont.italicSystemFont(ofSize: size))}
    
    ///文本颜色
    public static func color(_ color:UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textColor = color
        })
    }
    ///阴影颜色
    public static func shadow(_ color:UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.shadowColor = color
        })
    }
    ///阴影偏移
    public static func shadow(offset: CGSize) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.shadowOffset = offset
        })
    }
    ///对齐
    public static func align(_ align:NSTextAlignment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.textAlignment = align
        })
    }
    ///居中对齐
    public static var center: UCSetting<TargetType> {
        return self.align(.center)
    }
    ///左对齐
    public static var left: UCSetting<TargetType> {
        return self.align(.left)
    }
    ///右对齐
    public static var right: UCSetting<TargetType> {
        return self.align(.right)
    }
    ///换行模式
    public static func `break`(mode: NSLineBreakMode) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.lineBreakMode = mode
        })
    }
    ///换行策略
    @available(iOS 14.0, *)
    public static func `break`(strategy: NSParagraphStyle.LineBreakStrategy) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.lineBreakStrategy = strategy
        })
    }
    ///最大显示行数
    public static func lines(_ lines:Int = 0) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.numberOfLines = lines
        })
    }
    ///baselineAdjustment
    public static func baseline(_ adjust: UIBaselineAdjustment) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.baselineAdjustment = adjust
        })
    }
    ///字体最小比例
    public static func min(scale:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsFontSizeToFitWidth = true
            target.minimumScaleFactor = scale
        })
    }
    ///字体最小尺寸
    public static func min(size:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.adjustsFontSizeToFitWidth = false
            target.minimumScaleFactor = size
        })
    }
    ///启用
    public static func enabled(_ enabled:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isEnabled = enabled
        })
    }
    ///高亮
    public static func highlighted(_ enabled:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isHighlighted = enabled
        })
    }
    ///高亮颜色
    public static func highlighted(color: UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.highlightedTextColor = color
        })
    }
    ///allowsDefaultTighteningForTruncation
    public static func defaultTighteningForTruncation(_ allow: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsDefaultTighteningForTruncation = allow
        })
    }
    ///preferredMaxLayoutWidth
    public static func max(width: CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.preferredMaxLayoutWidth = width
        })
    }
    ///showsExpansionTextWhenTruncated
    public static func showExpansionTextWhenTruncated(_ show: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.showsExpansionTextWhenTruncated = show
        })
    }
}
